package com.yang.service.Impl;

import com.yang.exception.BusinessException;
import com.yang.mapper.SysUserMapper;
import com.yang.mapper.SysUserRoleMapper;
import com.yang.pojo.SysUser;
import com.yang.pojo.SysUserRole;
import com.yang.service.SysUserService;
import com.yang.utils.CommonUtil;
import com.yang.utils.Constants;
import com.yang.utils.JsonResult;
import com.yang.utils.MD5;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author 杨天可
 * @Date 2021/6/11 10:29
 * @Version 1.0
 */
@Service("sysUserService")
@Transactional
public class SysUserServiceImpl implements SysUserService {
    @Resource
    SysUserMapper sysUserMapper;
    @Resource
    RedisTemplate<String, Integer> redisTemplate;
    @Resource
    SysUserRoleMapper sysUserRoleMapper;

    @Override
    public SysUser findByUsername(String username) {
        return sysUserMapper.findByUsername(username);
    }


    @Override
    public Map<String, Object> regist(SysUser sysUser) {
        JsonResult jsonResult = new JsonResult();
        SysUser byUsername = sysUserMapper.findByPhone(sysUser.getPhone());
        if (byUsername != null) {
            throw new BusinessException("您已注册账号");
        }
        SysUser byUsername1 = sysUserMapper.findByUsername(sysUser.getUsername());
        if (byUsername1 != null && !byUsername1.getPhone().equals(sysUser.getPhone())) {
            throw new BusinessException("用户名已被注册");
        }
        SysUser sysUser1 = new SysUser();
        String salt = CommonUtil.createSalt();
        sysUser1.setUsername(sysUser.getUsername());
        sysUser1.setPhone(sysUser.getPhone());
        sysUser1.setEmail(sysUser.getEmail());
        sysUser1.setSalt(salt);
        sysUser1.setPassword(MD5.encodePassword(sysUser.getPassword(), salt));
        int insert = sysUserMapper.insert(sysUser1);
        if (insert > 0) {
            SysUser newUser = sysUserMapper.findByUsername(sysUser.getUsername());
            SysUserRole sysUserRole = new SysUserRole(newUser.getId(), Constants.ROLE_ID);
            sysUserRoleMapper.insert(sysUserRole);
            jsonResult.setCode("200");
            jsonResult.setMsg("注册成功");
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("注册失败");
        }
        return jsonResult.getValues();
    }

    @Override
    public Map<String, Object> checkCode(String username ,Integer code) {
        JsonResult jsonResult = new JsonResult();
        SysUser byUsername1 = sysUserMapper.findByUsername(username);
        if (byUsername1 != null) {
            /* 校验验证码*/
            Integer integer = redisTemplate.opsForValue().get(byUsername1.getEmail());
            if (integer == null || !integer.equals(code)) {
                throw new BusinessException("验证码错误");
            }
            jsonResult.setCode("200");
        }else{
            throw new BusinessException("用户不存在");
        }
        return jsonResult.getValues();
    }

    @Override
    public Map<String, Object> ChangePassword(String username, String password) {
        JsonResult jsonResult = new JsonResult();
        SysUser byUsername = sysUserMapper.findByUsername(username);
        /* 修改密码*/
        byUsername.setPassword(MD5.encodePassword(password, byUsername.getSalt()));
        int insert = sysUserMapper.updateByPrimaryKey(byUsername);
        if (insert > 0) {
            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功");
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("修改失败");
        }
        return jsonResult.getValues();
}

    @Override
    public Map<String, Object> updateSysUser(SysUser sysUser) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (sysUser.getId() == null) {
                throw new BusinessException("获取个人信息失败");
            }
            int result = sysUserMapper.updateByPrimaryKey(sysUser);
            if (result > 0) {
                jsonResult.setCode("200");
                jsonResult.setMsg("修改成功");
            } else {
                jsonResult.setCode("400");
                jsonResult.setMsg("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("系统开小差");
        }
        return jsonResult.getValues();
    }

    @Override
    public Map<String, Object> getSubject() {
        JsonResult jsonResult = new JsonResult();
        try {
            Subject subject = SecurityUtils.getSubject();
            SysUser user = (SysUser) subject.getPrincipal();
            if (user != null) {
                jsonResult.setCode("200");
                jsonResult.setMsg("获取成功");
                jsonResult.setData(user);
            } else {
                jsonResult.setCode("400");
                jsonResult.setMsg("获取失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("系统开小差");
        }
        return jsonResult.getValues();
    }
}
